package com.guigu.userservice.controller;

import com.guigu.userservice.entity.Permission;
import com.guigu.userservice.entity.RolePermission;
import com.guigu.userservice.service.PermissionService;
import com.guigu.userservice.service.RolePermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private RolePermissionService rolePermissionService;

    @GetMapping("/selectAllPermission")
    public List<Permission> selectAllPermission(){
        return permissionService.selectAllPermission();
    }

    @GetMapping("/selectPermissionById/{id}")
    public List<Permission> selectPermissionById(@PathVariable("id") Long id){
        List<RolePermission> rolePermissionList = rolePermissionService.getRolePermissionByRoleId(id);
        List<Permission> permissionList = new ArrayList<>();
        for (RolePermission r:rolePermissionList) {
            permissionList.add(permissionService.getById(r.getPermissionId()));
        }
        return permissionList;
    }
}
